var express = require('express');
var router = express.Router();
var async = require('async');

//mongodb数据库链接
const mongo = require('mongodb');
const MongoClient = require('mongodb').MongoClient
const mongoConfig = require('../config/mongodb');
var url = 'mongodb://' + mongoConfig.user + ':' + mongoConfig.password + '@' + mongoConfig.host + ':' + mongoConfig.port + '/' + mongoConfig.database + '?authMechanism=' + mongoConfig.authMechanism + '&authSource=' + mongoConfig.authSource;

//bson中间件引用
const BSON = require('bson');
const Long = BSON.Long;

//查询变电站
router.post('/search', function(req, res) {
    var param = req.body;
    let requirement = param.requirement;
    let lastRequirement = {}; //定义一个新对象
    for (let i in requirement) {
        let item = requirement[i];
        if (item != "" && i == 'crtime') {
            //把日期转换成时间戳
            let newCrtime = new Date(item).getTime() / 1000;
            lastRequirement[i] = newCrtime
            continue; //进入下次循环
        } else if (item != "") {
            //把非空数据添加到新对象中
            lastRequirement[i] = item;
        }
    }
    param.requirement = lastRequirement;
    try {
        async.auto({
            list: function(cb) {
                substationManageController.pageList(param, cb);
            },
            listLength: function(cb) {
                substationManageController.count(param, cb);
            }
        }, function(err, data) {
            res.send({
                code: "200",
                msg: "获取详情成功",
                result: data.list,
                total: data.listLength
            });
        });
    } catch (err) {
        res.end(err.stack);
    }
});

module.exports = router;